package com.lucan.tracking;

import android.content.Context;

import com.lucan.tracking.api.ExecutorUtil;
import com.lucan.tracking.api.Track;
import com.lucan.tracking.api.executor.Priority;

import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.PriorityBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author qihailong
 * @date 2018/7/8 上午10:03
 * @description TODO
 */

public class TKMonitor {

    public static String BASE_URL = "";

    private Track mTrackNow;
    private Track mTrackLater;

    private Context mContext;
    private volatile boolean mIsInitialized = false;

    private static volatile TKMonitor mInstance;

    public static TKMonitor getInstance() {
        if (mInstance == null) {
            synchronized (TKMonitor.class) {
                if (mInstance == null) {
                    mInstance = new TKMonitor();
                }
            }
        }
        return mInstance;
    }


    private TKMonitor() {
        ExecutorService executor = ExecutorUtil.crateIOExecutor();
        mTrackNow = new Track(executor, Priority.HIGH);
        mTrackLater = new Track(executor, Priority.LOW);
    }

    /**
     * @param context
     * @param baseUrl
     */
    public void init(Context context, String baseUrl) {
        if (mIsInitialized) {
            return;
        } else {
            mIsInitialized = false;
        }
        mContext = context.getApplicationContext();
        BASE_URL = baseUrl;
    }

    /**
     * @param context
     */
    public void init(Context context) {
        this.init(context, "");
    }

    /**
     * 立即发送
     *
     * @param url
     * @param params
     * @param isPost
     */
    public void sendTrackNow(String url, Map<String, String> params, boolean isPost) {
        mTrackNow.sendTrack(url, params, isPost);
    }

    /**
     * 延迟发送
     *
     * @param url
     * @param params
     * @param isPost
     */
    public void sendTrackLater(String url, Map<String, String> params, boolean isPost) {
        mTrackLater.sendTrack(url, params, isPost);
    }

}
